<?php
namespace Console;
use Swiftx\Http\Post;
use Swiftx\Http\Session;

/**
+---------------------------------------------------------------------------------------------------------------
 * 控制台控制器类
+---------------------------------------------------------------------------------------------------------------
 * @author		胡永强  <odaytudio@gmailcom>
 * @since		2015-02-25
 * @copyright	Copyright (c) 2014-2015 Swiftx Inc.
+---------------------------------------------------------------------------------------------------------------
 */
class Home {

    /**
     * 登陆处理
     */
    public static function ActionLogin(){
        // 校验图片验证码
        if(Common::CheckVerify(Post::Value('captcha'))){
            die(json_encode(array('code'=>503,'message'=>'验证码不正确')));
        }
        // 成功执行登陆
        else if($admin = $GLOBALS->DB->Table('Table_User_Player')->Where('Username',Post::Value('username'))->Row){
            if($admin['Password'] == Post::Value('password')){
                Session::Set('Admin', $admin['ID']);
                die(json_encode(array('code'=>200)));
            }
            die(json_encode(array('code'=>502)));
        } else {
            die(json_encode(array('code'=>501)));
        }
    }

    /**
     * 用户状态校验
     */
    public static function ActionStatus(){
        // 用户已登录
        if(Session::Exists('User.Member')){
            // 获取用户信息
            $member = $GLOBALS->DB->Table('es_member')->Where('member_id',Session::Value('User.Member'))->Row;
            // 构造用户结构
            $result['Code'] = 200;
            $result['ID'] = $member['member_id'];
            $result['TrueName'] = $member['name'];
            $result['Mobile'] = $member['mobile'];
            $result['Face'] =  'http://test.fam520.com/statics/'.substr($member['face'],4);
            // 写入Cookie
            $postTime = time()+3600;
            setcookie('Member::ID',$result['ID'],$postTime,'/','.fam520.com');
            setcookie('Member::TrueName',$result['TrueName'],$postTime,'/','.fam520.com');
            setcookie('Member::Mobile',$result['Mobile'],$postTime,'/','.fam520.com');
            setcookie('Member::Face',$result['Face'],$postTime,'/','.fam520.com');
            echo json_encode($result);
        }
        // 用户未登录
        else{
            setcookie('Member::ID',null,0,'/','.fam520.com');
            setcookie('Member::TrueName',null,0,'/','.fam520.com');
            setcookie('Member::Mobile',null,0,'/','.fam520.com');
            setcookie('Member::Face',null,0,'/','.fam520.com');
            echo json_encode(array('Code'=>201));
        }
    }
}